﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public class YCAFS05Report : BaseReport
    { 
 
        INTEXDBC INTEXDBC;
        public YCAFS05Report()
            : base()
        {
        }
        public YCAFS05Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            if (dicParamater.ContainsKey("ROOTDRIVE"))
            {
                Root = dicParamater["ROOTDRIVE"].ToString();
            }
            else
            {
                Root = "C:\\IntexReport";
            }
       
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
        }
        public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                /*FSSART	FSABEZ	COLOR	SIZE1	MTYPE	FSSMART	WABEZ	WALOHNA	LSNAME1	USEAGE	FSSVBME	CONSUMPTION   */
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["ICC_FS05"];
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("FSSART", typeof(System.String));
                dtOutput.Columns.Add("FSABEZ", typeof(System.String));
                dtOutput.Columns.Add("COLOR", typeof(System.String));
                dtOutput.Columns.Add("SIZE1", typeof(System.String));
                dtOutput.Columns.Add("TYPE", typeof(System.String));
                dtOutput.Columns.Add("MTYPE", typeof(System.String));
                dtOutput.Columns.Add("FSSMART", typeof(System.String));
                dtOutput.Columns.Add("WABEZ", typeof(System.String));
                dtOutput.Columns.Add("WALOHNA", typeof(System.String));
                dtOutput.Columns.Add("LSNAME1", typeof(System.String));
                dtOutput.Columns.Add("USEAGE", typeof(System.String));
                dtOutput.Columns.Add("FSSVBME", typeof(System.String));
                dtOutput.Columns.Add("CONSUMPTION", typeof(System.String));
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);
                
                exAgent.DataTableToExcel(dtOutput, 3, 1);

                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = false;
            try
            {
                Helper.GetDate(ref date, ref year, ref month, ref day);
                strExcelFile = @"YCA\YCAStandard.xls";
                Standard = "FS05";
                destfolder = SetFolder(Root, "款式BOM");
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\YCA\YCAStandard.xls");
               string newfilename = @"款号BOM" + date; 
                exAgent.CheckWorkBook(newfilename);
                LocalFile(newfilename);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        private string CreateFile(string filename)
        { 
            string sheetName = "";
            string pwd = ""; 
            try
            {
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                sheetName = filename;
                exAgent.CreateNewSheet(filename, Standard, sheetName.Replace(".xls", ""), pwd);
                filename = exAgent.FileName; 
                exAgent.SetCurrentSheet(1);
                FillInExcelData(columns, dsSource, 3, 1);
                filename = exAgent.FileName; 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                exAgent.Save();
                exAgent.Close();
            }
            return filename;
        }

        private void LocalFile(string ordernumer)
        {
        
            string fileabsname = ordernumer + ".xls";
            string localFile = @"C:\Windows\Temp\" + fileabsname;
            string tempfile = "";
            try
            {
                tempfile = CreateFile(fileabsname);
                localFile = destfolder + "\\" + fileabsname;
                if (System.IO.File.Exists(localFile))
                {
                    System.IO.File.Delete(localFile);
                }
                File.Move(tempfile, localFile);
                System.Diagnostics.Process.Start("EXCEL.EXE", localFile);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
    }
}
